# coding:utf-8
import os

from utils.common.yamlData import get_yaml_data
from config.confManage import dir_manage


def getFilePathList(path, filetype):
    pathList = []
    for root, dirs, files in os.walk(path):
        for file in files:
            if file.endswith(filetype):
                pathList.append(os.path.join(root, file))
    return pathList


def write_case(_path, filetype="yml"):
    yml_list = getFilePathList(_path, filetype)
    path = dir_manage(dir_manage('${pro_dir}$') + dir_manage('${page_dir}$'))
    with open(path + r"App_pages_01.py", 'w+', encoding='utf-8') as f:
        f.write("from appium.webdriver.common.mobileby import MobileBy\n\n")
        f.write("from utils.common.basepage import pyappium \n\n")

        for casefile in yml_list:
            yml_data = get_yaml_data(casefile)
            f.write("""
class App_{testtitle}(pyappium):""".format(testtitle=yml_data["title"]))
            for casedata in yml_data["testcase"]:
                f.write("""
    def {casename}(self):
        """.format(casename=casedata["casename"]))
                for case in casedata["case"]:
                    loc_mehtod = case["locator"].split("->")[0]
                    path = case["locator"].split("->")[1]
                    if case["method"] == "input":
                        f.write(
                            """self.{method}((MobileBy.{loc_mehtod}, "{locator}"),"{input_text}")\n        """.format(
                                input_text=case["text"],
                                method=case["method"],
                                loc_mehtod=loc_mehtod,
                                locator=path)
                        )
                    else:
                        f.write(
                            """self.{method}((MobileBy.{loc_mehtod}, "{locator}"))\n        """.format(
                                method=case["method"],
                                loc_mehtod=loc_mehtod,
                                locator=path))


if __name__ == '__main__':
    ym_path = r'D:\apptest\testSuite\datas\appDatas'
    write_case(ym_path)
